<template>
  <view class="">
    <my-search></my-search>
    <view class="cate-container">
      <!-- 左侧一级分类 -->
      <scroll-view scroll-y="true" class="left-view" :style="{height: wh + 'px'}">
        <block v-for="(item, i) in cateList" :key="item.cat_id">
          <view :class="[ i === active ? 'active' : '']" @click="cateChanged(i)">{{item.cat_name}}</view>
        </block>
      </scroll-view>
      <!-- 右侧二三级分类 -->
      <scroll-view scroll-y="true" class="right-view" :style="{height: wh + 'px'}" :scroll-top="scrollTop">
        <view class="sub-cate-list" v-for="item in subCateList" :key="item.cat_id">
          <!-- 二级分类标题 -->
          <view class="sub-title">/ {{item.cat_name}} /</view>
          <!-- 三级分类列表 -->
          <view class="lv3-list">
            <navigator class="lv3-item" v-for="(lv3, i) in item.children" :key="i" :url="'/packageGoods/goods_list/goods_list?cid=' + lv3.cat_id" hover-class="none">
              <image :src="lv3.cat_icon" mode=""></image>
              <text>{{lv3.cat_name}}</text>
            </navigator>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  import mix from '@/mixins/tabbar-badge.js'
  export default {
    mixins: [mix],
    data() {
      return {
        // 可用窗口的高度
        wh: 0,
        // 选中项的索引
        active: 0,
        // 分类列表数据
        cateList: [],
        // 二级分类列表
        subCateList: [],
        // 右侧 scroll-view 滚动条的位置
        scrollTop: 0
      }
    },
    onLoad() {
      // 动态获取窗口可用高度
      const sysInfo = uni.getSystemInfoSync()
      this.wh = sysInfo.windowHeight - 52

      // 获取分类数据
      this.getCateList()
    },
    methods: {
      // 获取分类数据
      async getCateList() {
        const { data: res } = await uni.$http.get('/api/public/v1/categories')
        if (res.meta.status !== 200) return uni.$showError()
        this.cateList = res.message
        this.subCateList = res.message[0].children
      },
      // 一级分类选中项改变
      cateChanged(i) {
        this.active = i
        this.subCateList = this.cateList[i].children
        this.scrollTop = this.scrollTop === 0 ? 1 : 0
      }
    }
  }
</script>

<style lang="scss">
  .cate-container {
    display: flex;

    .left-view {
      width: 120px;
      height: 400px;

      view {
        line-height: 60px;
        text-align: center;
        font-size: 12px;
        background-color: #F7F8FA;
      }

      .active {
        background-color: #FFFFFF;
        position: relative;

        &:after {
          content: ' ';
          display: block;
          width: 4px;
          height: 25px;
          background-color: #C00000;
          position: absolute;
          top: 50%;
          transform: translateY(-50%);
        }
      }
    }
  }

  .sub-title {
    text-align: center;
    font-weight: bold;
    font-size: 12px;
    padding-top: 20px;
  }

  .lv3-list {
    display: flex;
    flex-wrap: wrap;

    .lv3-item {
      display: flex;
      width: 33.33%;
      flex-direction: column;
      font-size: 12px;
      align-items: center;
      margin: 10px 0;

      image {
        width: 120rpx;
        height: 120rpx;
      }
    }
  }
</style>
